import { createStore } from 'vuex'
import createPersistedState from 'vuex-persistedstate'
// import Vuex from 'vuex'
// import createPersistedState from "vuex-persistedstate";
// Vue.use(Vuex)
const Types = {
  SET_TOKEN: 'SET_TOKEN',
  SET_USER: 'SET',
  SET_USERavatar: 'SETAVATAR',
  SET_USERrealName: 'SETRealName'
}
export default createStore({
  state: {
    isToken: false,
    user: {
      username: '',
      avatar: '',
      status: '',
      id: '',
      realname: '',
      email: ''
    },
    playConfig: {
      isplay: false,
      playImg: '',
      playSinger: '',
      playSongName: '',
      totalTime: '',
      currentTime: '',
      playSrc: ''
    }
  },

  getters: {
    isToken: state => state.isToken,
    user: state => state.user,
    playInfo: state => state.playConfig
  },

  mutations: {
    changePlayInfo(state, songInfo) {
      state.playConfig = songInfo
    },
    changeCurrentTime(state) {
      state.playConfig.currentTime = parseInt(state.playConfig.currentTime + '') + 100 + ''
    },
    [Types.SET_TOKEN](state, payload) {
      if (payload) {
        state.isToken = payload
      } else {
        state.isToken = false
      }
    },
    [Types.SET_USER](state, payload) {
      ;(state.user.username = payload.username),
        (state.user.avatar = payload.avatar),
        (state.user.status = payload.status),
        (state.user.id = payload.id),
        (state.user.realname = payload.realname)
      state.user.email = payload.email
    },
    [Types.SET_USERavatar](state, payload) {
      state.user.avatar = payload
    },
    [Types.SET_USERrealName](state, payload) {
      state.user.realname = payload
    }
  },

  actions: {
    setUser({ commit }, payload) {
      commit(Types.SET_USER, payload)
    },
    setUserAvatar({ commit }, payload) {
      commit(Types.SET_USERavatar, payload)
    },
    setUserRealName({ commit }, payload) {
      commit(Types.SET_USERrealName, payload)
    },
    setToken({ commit }, payload) {
      commit(Types.SET_TOKEN, payload)
    }
  },
  plugins: [createPersistedState()]
})
